/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "spatial_dump.h"

#include <cinttypes>
#include <ctime>
#include <queue>

#include "spatial_awareness_common.h"
#include "spatial_awareness_mgr_service.h"

namespace OHOS {
namespace Msdp {
namespace {
auto g_service = DelayedSpSingleton<SpatialaWarenessMgrService>::GetInstance();
constexpr uint32_t MS_NS = 1000000;
}
void SpatialDump::DumpHelp(int32_t fd)
{
    dprintf(fd, "Usage:\n");
    dprintf(fd, "      -h: dump help\n");
    dprintf(fd, "      -s: dump the spatial map info\n");
}

bool SpatialDump::DumpSpatiaHelp(int32_t fd, const std::vector<std::u16string> &args)
{
    if ((args.empty()) || (args[0].compare(u"-h") != 0)) {
        SPATIAL_LOGE(MODULE_SERVICE, "%{public}s args cannot be empty or invalid", __func__);
        return false;
    }
    DumpHelp(fd);
    return true;
}

bool SpatialDump::DumpSpatialMap(int32_t fd, const std::vector<std::u16string> &args)
{
    if ((args.empty()) || (args[0].compare(u"-s") != 0)) {
        SPATIAL_LOGE(MODULE_SERVICE, "%{public}s args cannot be empty or invalid", __func__);
        return false;
    }
    DumpCurrentTime(fd);
    auto map = g_service->GetCoordinateSystem()->GetSpatialMap();
    dprintf(fd, "center deviceId:%s | deviceName:%s | deviceType:%d\n",
        map.center.deviceId.c_str(), map.center.deviceName.c_str(), map.center.typeId);
    int count = 1;

    if (map.map.empty()) {
        return false;
    }

    for (auto iter : map.map) {
        dprintf(fd,
                "remote device:%d | deviceId:%s | deviceName:%s | deviceType:%d | direction:%d | distance:%f"
                "| angle:%f | innerouter:%d | exist:%d\n",
                count, iter.first.deviceId.c_str(), iter.first.deviceName.c_str(),
                iter.first.typeId, iter.second.direction, iter.second.distance,
                iter.second.angle, iter.second.innerouter, iter.second.exist);
        count = count + 1;
    }
    return true;
}

void SpatialDump::DumpCurrentTime(int32_t fd)
{
    timespec curTime = { 0, 0 };
    clock_gettime(CLOCK_REALTIME, &curTime);
    struct tm *timeinfo = localtime(&(curTime.tv_sec));
    if (timeinfo == nullptr) {
        SPATIAL_LOGE(MODULE_SERVICE, "%{public}s timeinfo cannot be null", __func__);
        return;
    }
    dprintf(fd, "Current time: %02d:%02d:%02d.%03d\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec,
            int32_t { (curTime.tv_nsec / MS_NS) });
}
}  // namespace Msdp
}  // namespace OHOS